do "E:\yungyu\lottery_baby\do\set_environment.do"

clear

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
keep if inrange(prize10M,0.1,5) //1M - 50M

reghdfe totbaby, a(age) resid 
predict totbaby_dage, residual
sum totbaby
replace totbaby_dage = totbaby_dage + r(mean)

reghdfe totbaby, a(age year) resid 
predict totbaby_dageyear, residual
sum totbaby
replace totbaby_dageyear = totbaby_dageyear + r(mean)

gen givebirth = baby > 0
reghdfe givebirth, a(age) resid 
predict givebirth_dage, residual
sum givebirth
replace givebirth_dage = givebirth_dage + r(mean)

reghdfe givebirth, a(age year) resid 
predict givebirth_dageyear, residual
sum givebirth
replace givebirth_dageyear = givebirth_dageyear + r(mean)
	
gen n = 1	
collapse (count)n (mean)totbaby totbaby_dage totbaby_dageyear givebirth givebirth_dage givebirth_dageyear, by(norm current win_year)
	
sort win_year current norm 

save "$table\temp\TabG1.dta", replace

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
keep if inrange(prize10M,0.1,5) //1M - 50M

reghdfe totbaby, a(age) resid 
predict totbaby_dage, residual
sum totbaby
replace totbaby_dage = totbaby_dage + r(mean)

reghdfe totbaby, a(age year) resid 
predict totbaby_dageyear, residual
sum totbaby
replace totbaby_dageyear = totbaby_dageyear + r(mean)

gen givebirth = baby > 0
reghdfe givebirth, a(age) resid 
predict givebirth_dage, residual
sum givebirth
replace givebirth_dage = givebirth_dage + r(mean)

reghdfe givebirth, a(age year) resid 
predict givebirth_dageyear, residual
sum givebirth
replace givebirth_dageyear = givebirth_dageyear + r(mean)
	
gen n = 1	
collapse (count)n (mean)totbaby totbaby_dage totbaby_dageyear givebirth givebirth_dage givebirth_dageyear, by(norm current)
	
ap using "$table\temp\TabG1.dta"

order win_year
tostring win_year, replace
replace win_year = "All" if win_year == "."

sort win_year current norm 

export excel "$table/Tables_Max50M.xlsx", sheet("TabG1") sheetreplace first(var)

mata
	b = xl()
	b.load_book("$table/Tables_Max50M.xlsx")
	b.set_sheet("TabG1")

	b.set_column_width(1,10,12)
	
	rows = (1,150)
	cols = (1,10)
	b.set_horizontal_align(rows, cols, "center")
	
	rows = (2,150)
	cols = (5,10)
	b.set_number_format(rows, cols, "0.000")
	
end

keep if win_year == "All"

reshape wide n totbaby* givebirth*, i(win_year norm) j(current)

sum totbaby1 if norm == -1
loc m0 = r(mean)
sum totbaby1 if norm == 6
loc m1 = r(mean)
loc beta = `m1'-`m0'
loc beta: dis %4.3f `beta'

twoway (connect totbaby1 norm, color(black)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Mean Cumulative Number of Children) ///
		ylabel(,angle(0) format(%4.3fc)) xlabel(-3(1)6) ///
		legend(off) ///
		text(1.039 4 "beta_FD pooled = `beta'")
graph export "$figure/Fig3.1b_totbaby_1Mup.png", as(png) replace


forv i = 0(1)1{
	sum totbaby`i' if norm == -1
	loc m0 = r(mean)
	sum totbaby`i' if norm == 6
	loc m1 = r(mean)
	loc beta`i' = `m1'-`m0'
	loc beta`i': dis %4.3f `beta`i''
}
loc beta = `beta1' - `beta0'
loc beta`i': dis %4.3f `beta'
	
twoway 	(connect totbaby0 norm, ms(Sh) lp(dash) color(gs8)) ///
		(connect totbaby1 norm, color(black)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Mean Cumulative Number of Children) ///
		ylabel(,angle(0) format(%4.2fc)) xlabel(-3(1)6) ///
		legend(order(2 "Current Winners" 1 "Future Winners") size(small)) ///
		text(1.02 3.5 "beta_FD Trteated = `beta1'") ///
		text(0.95 1 "beta_FD Control = `beta0'", color(gs8)) ///
		text(1 4.5 "beta_DD = `beta'", color(navy))
graph export "$figure/Fig3.1c_totbaby_1Mup.png", as(png) replace
	

sum givebirth_dageyear1 if norm == -1
loc m0 = r(mean)
sum givebirth_dageyear1 if norm >= 1
loc m1 = r(mean)
loc beta = `m1'-`m0'
loc beta: dis %4.3f `beta'	
	
twoway 	(connect givebirth_dageyear1 norm, color(black)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Mean Probability of Giving Birth) ///
		ylabel(,angle(0) format(%4.3fc)) xlabel(-3(1)6) ///
		legend(off) ///
		text(0.055 3 "beta_FD pooled = `beta'")
graph export "$figure/Fig3.1b_givebirth_1Mup.png", as(png) replace
		

forv i = 0(1)1{
	sum givebirth`i' if norm == -1
	loc m0 = r(mean)
	sum givebirth`i' if norm >= 1
	loc m1 = r(mean)
	loc beta`i' = `m1'-`m0'
	loc beta`i': dis %4.3f `beta`i''
}
loc beta = `beta1' - `beta0'
loc beta`i': dis %4.3f `beta'
			
		
twoway 	(connect givebirth0 norm, ms(Sh) lp(dash) color(gs8)) ///
		(connect givebirth1 norm, color(black)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Mean Probability of Giving Birth) ///
		ylabel(,angle(0) format(%4.2fc)) xlabel(-3(1)6) ///
		legend(order(2 "Current Winners" 1 "Future Winners") size(small)) ///
		text(0.055 3 "beta_FD Trteated = `beta1'") ///
		text(0.032 3 "beta_FD Control = `beta0'", color(gs8)) ///
		text(0.06 5 "beta_DD = `beta'", color(navy))
graph export "$figure/Fig3.1c_givebirth_1Mup.png", as(png) replace








use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
keep if inrange(prize10M,0.1,5) //1M - 50M

forv i = 96(1)101{
	reghdfe totbaby if win_year == `i', a(age) resid 
	predict totbaby_r, residual
	sum totbaby if win_year == `i'
	replace totbaby = totbaby_r + r(mean) if win_year == `i'
	drop totbaby_r
}
gen givebirth = baby > 0
forv i = 96(1)101{
	reghdfe givebirth if win_year == `i', a(age) resid 
	predict givebirth_r, residual
	sum givebirth if win_year == `i'
	replace givebirth = givebirth_r + r(mean) if win_year == `i'
	drop givebirth_r
}

collapse (mean)totbaby givebirth, by(win_year norm current)
reshape wide totbaby givebirth, i(norm win_year) j(current)

sum totbaby1 if norm == -1
loc m0 = r(mean)
sum totbaby1 if norm == 6
loc m1 = r(mean)
loc beta = `m1'-`m0'
loc beta: dis %4.3f `beta'

twoway 	(connect totbaby0 norm if win_year == 96, ms(Sh) lp(dash) color(gs8)) ///
		(connect totbaby1 norm if win_year == 96, color(black)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Mean Cumulative Number of Children) ///
		ylabel(,angle(0) format(%4.2fc)) xlabel(-3(1)6) ///
		legend(order(2 "Current Winners" 1 "Future Winners") size(small)) 

********** "E:\yungyu\lottery_baby\do\JoLE R&R\Golosov\Fig3.2_1Mup.do"

do "E:\yungyu\lottery_baby\do\set_environment.do"

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

clear

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
keep if inrange(prize10M,0.1,5) //1M - 50M

gen givebirth = baby > 0

gen notbase = norm != -1
gen Z = current == 1 & notbase == 1

foreach y in totbaby givebirth{
    cap rm "$table/temp/Fig3.2_1Mup_`y'.xls"
    cap rm "$table/temp/Fig3.2_1Mup_`y'.txt"
	
	* Pooled Results
	forv i = -3(1)6{
		if `i' != -1{
			reghdfe `y' current notbase Z $control1 $control2 $control3 if norm == -1 | norm == `i' , a(age year) cl(houseid)
			outreg2 using "$table/temp/Fig3.2_1Mup_`y'.xls" , append bd(3) sd(3) nocon keep(Z) ctitle(`y',T`i',Pooled)

		}
	}
	
	* Cohort by cohort
	forv c = 96(1)101{
		forv i = -3(1)6{
			if `i' != -1{
				reghdfe `y' current notbase Z $control1 $control2 $control3 if (norm == -1 | norm == `i') & win_year == `c', a(age year) cl(houseid)
				outreg2 using "$table/temp/Fig3.2_1Mup_`y'.xls" , append bd(3) sd(3) nocon keep(Z) ctitle(`y',T`i',C`c')

			}
		}
	}
}

foreach y in totbaby givebirth{
	import delimited using "$table/temp/Fig3.2_1Mup_`y'.txt", clear

	sxpose, clear
	rename _var6 b
	rename _var7 se
	rename _var3 t
	rename _var4 cohort
	rename _var9 n

	keep cohort t b se n

	drop in 1

	gen b_destring = subinstr(b,"*","",.)
	destring b_destring, replace
	replace n = subinstr(n,",","",.)
	destring n, replace

	gen v1 = b_destring if cohort != "Pooled"
	replace v1 = v1 * n
	egen b_weighted = sum(v1), by(t)
	sum n if cohort == "Pooled"
	replace b_weighted = b_weighted/r(mean)
	tostring b_weighted, format(%4.3f) replace force
	tostring n, format(%9.0fc) replace force

	keep if cohort == "Pooled"
	keep t b se n b_weighted

	save "$table/temp/Fig3.2_1Mup_`y'.dta", replace
}

use "$table/temp/Fig3.2_1Mup_totbaby.dta", clear
loc N = _N+1
set obs `N'
ap using "$table/temp/Fig3.2_1Mup_givebirth.dta"
gen row = _n
loc N = _N+2
set obs `N'
recode row . = 0
sort row
drop row

replace t = "Event Time" in 1
replace b = "Beta^DD (Pooled)" in 1
replace se = "SE^DD (Pooled)" in 1
replace n = "# of Observations" in 1
replace b_weighted = "Beta^DD (Cohort-Weighted)" in 1

replace t = "Panel A: Cumulative Number of Children" in 2
replace t = "Panel B: Give Birth in a Given Year" in 12

export excel "$table/Tables_Replicate_Golosov.xlsx", sheet("Fig3.2 Above 1M") sheetreplace

mata
	b = xl()
	b.load_book("$table/Tables_Replicate_Golosov.xlsx")
	b.set_sheet("Fig3.2 Above 1M")

	b.set_column_width(1,1,12)
	b.set_column_width(2,4,20)
	b.set_column_width(5,5,25)
	
	rows = (1,100)
	cols = (2,5)
	b.set_horizontal_align(rows, cols, "center")
	
	b.set_font_bold(2, 1, "on")
	b.set_font_bold(12, 1, "on")
	
	
	rows = (2,2)
	cols = (1,5)
	b.set_sheet_merge("Fig3.2 Above 1M", rows, cols) 
	rows = (12,12)
	b.set_sheet_merge("Fig3.2 Above 1M", rows, cols) 

	
end

use "$table/temp/Fig3.2_1Mup_totbaby.dta", clear

replace t = substr(t,2,.)
replace b = subinstr(b,"*","",.)
replace se = subinstr(se,"(","",.)
replace se = subinstr(se,")","",.)
drop n
destring _all, replace
loc n = _N + 1
set obs `n'
recode t . = -1
foreach var in b se b_weighted{
	recode `var' . = 0
}

gen upper = b + 1.96 * se
gen lower = b - 1.96 * se
sort t

twoway 	(rline upper lower t, lp(dash) color(gs8)) ///
		(connect b t, color(black)) ///
		(connect b_weighted t, color(blue) ms(Oh) lp(dash)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Effect on Cumulative Number of Children) ///
		ylabel(-0.03(0.03)0.12,angle(0) format(%4.2fc)) xlabel(-3(1)6) ///
		legend(col(2) order(2 "Point Estimate (Pooled)" 1 "95% CI" 3 "Point Estimate (Cohort-Weighted)") size(small)) ///
		yline(0,lc(black))
graph export "$figure/Fig3.2_1Mup_totbaby.png", as(png) replace



use "$table/temp/Fig3.2_1Mup_givebirth.dta", clear

replace t = substr(t,2,.)
replace b = subinstr(b,"*","",.)
replace se = subinstr(se,"(","",.)
replace se = subinstr(se,")","",.)
drop n
destring _all, replace
loc n = _N + 1
set obs `n'
recode t . = -1
foreach var in b se b_weighted{
	recode `var' . = 0
}

gen upper = b + 1.96 * se
gen lower = b - 1.96 * se
sort t

twoway 	(rline upper lower t, lp(dash) color(gs8)) ///
		(connect b t, color(black)) ///
		(connect b_weighted t, color(blue) ms(Oh) lp(dash)), ///
		scheme(s1color) xtitle(Years from Lottery Prize) ///
		ytitle(Effect on Probability of Giving Birth) ///
		ylabel(-0.02(0.01)0.05,angle(0) format(%4.2fc)) xlabel(-3(1)6) ///
		legend(col(2) order(2 "Point Estimate (Pooled)" 1 "95% CI" 3 "Point Estimate (Cohort-Weighted)") size(small)) ///
		yline(0,lc(black))
graph export "$figure/Fig3.2_1Mup_givebirth.png", as(png) replace


mata
	b = xl()
	b.load_book("$table/Tables_Replicate_Golosov.xlsx")
	b.set_sheet("Fig3.2 Above 1M")
	
	b.put_string(1, 7, "Fig 3.2 DID Event Study Estimates")
	b.put_string(2, 7, "Outcomes: Cumulative Number of Children")
	b.put_picture(3, 7, "$figure/Fig3.2_1Mup_totbaby.png")
	
	b.put_string(1, 19, "Fig 3.1(b) First-Difference (Pooled Across Cohorts)")
	b.put_string(2, 19, "Outcomes: Give Birth in a Given Year")
	b.put_picture(3, 19, "$figure/Fig3.2_1Mup_givebirth.png")

end

********** "E:\yungyu\lottery_baby\do\JoLE R&R\Golosov\Fig3.2_1Mup _ bootstrap.do"

do "E:\yungyu\lottery_baby\do\set_environment.do"

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

clear

qui{
	clear
	set obs 6 //N of cohort
	gen win_year = 95 + [_n]
	expand 2
	bysort win_year: gen y = _n
	tostring y, replace
	replace y = "givebirth" if y == "1"
	replace y = "totbaby" if y == "2"
	expand 9
	bysort y win_year: gen parm = _n
	gen coef = .
	gen se = .
	gen n = .
	expand 1001
	bysort y win_year parm: gen id = _n-1

	sort id y win_year parm

	save "$wdata\Golosov_Fig3.2_bootstrap.dta", replace
}

forv a = 0(1)1000{ //Bootstrap for 1,000 times
	
	if (`a'>0) dis "-- Bootstrap `a'..."
	{
	set seed `a'

	use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	keep if inrange(prize10M,0.1,5) //1M - 50M
	
	if (`a'>0) {
		keep if norm == 0
		keep win_year current houseid 
		bsample, str(win_year current)
		joinby houseid using "$wdata\project_lottery_fertility_b3a6_first_5k.dta"
	}
	
	gen v1 = pers_real_prize_aftertax if norm == 0
	egen prize10M_realized = mean(v1), by(houseid)
	replace prize10M_realized = prize10M_realized/10000
	replace prize10M_realized = 0 if norm < 0
	recode prize10M_realized . = 0
	gen givebirth = baby > 0

	gen notbase = norm != -1
	gen Z = current == 1 & notbase == 1

	foreach y in givebirth totbaby {
  
		* Cohort by cohort
		forv c = 96(1)101{
			forv i = -3(1)6{
				if (`i' < 0) loc j = `i'+4
				if (`i' >= 0) loc j = `i'+3
				if `i' != -1{
					reghdfe `y' Z current notbase $control1 $control2 $control3 if (norm == -1 | norm == `i') & win_year == `c', a(age year) cl(houseid)
					
					local n`c' = e(N)
					matrix b = e(b)
					matrix V = e(V)
					
					local coef`y'_`c'_`j' = b[1,1]
					local se`y'_`c'_`j' =  sqrt(V[1,1])		

				}
			}
		}
	}
	
	use "$wdata\Golosov_Fig3.2_bootstrap.dta", clear

	foreach y in givebirth totbaby {	
		forv c = 96(1)101{
			replace n = `n`c'' if win_year == `c' & id == `a'
			forv i = 1(1)9{
				replace coef = `coef`y'_`c'_`i'' if y == "`y'" & win_year == `c' & parm == `i' & id == `a'
				replace se = `se`y'_`c'_`i'' if y == "`y'" & win_year == `c' & parm == `i' & id == `a'
			}
		}
	}

	save "$wdata\Golosov_Fig3.2_bootstrap.dta", replace
	}
}

use "$wdata\Golosov_Fig3.2_bootstrap.dta", clear

collapse (mean)coef [fw=n], by(y parm id)
drop if coef == .

replace parm = parm - 3

gen b2 = coef if id == 0
gen se2 = coef if id != 0

collapse (mean)b (sd)se, by(y parm)

gen row = _n
replace row = row + 20 if y == "givebirth"
loc N= _N+3
set obs `N'
replace row = 0 if row == .
replace row = 20 if _n == _N

sort row
drop row
	
save "$wdata\Golosov_Fig3.2_bootstrap_result.dta", replace


use "$table/temp/Fig3.2_1Mup_totbaby.dta", clear
loc N = _N+1
set obs `N'
ap using "$table/temp/Fig3.2_1Mup_givebirth.dta"
gen row = _n
loc N = _N+2
set obs `N'
recode row . = 0
sort row
drop row

merge 1:1 _n using "$wdata\Golosov_Fig3.2_bootstrap_result.dta"
tostring b2 se2, replace format(%4.3f) force
drop _m

replace t = "Event Time" in 1
replace b = "Beta^DD (Pooled)" in 1
replace se = "SE^DD (Pooled)" in 1
replace n = "# of Observations" in 1
replace b2 = "Beta^DD (Cohort-Weighted)" in 1
replace se2 = "SE^DD (Cohort-Weighted)" in 1

drop b_w y parm

replace t = "Panel A: Cumulative Number of Children" in 2
replace t = "Panel B: Give Birth in a Given Year" in 12

export excel "$table/Tables_Max50M.xlsx", sheet("TabG2") sheetreplace

mata
	b = xl()
	b.load_book("$table/Tables_Max50M.xlsx")
	b.set_sheet("TabG2")

	b.set_column_width(1,1,12)
	b.set_column_width(2,4,20)
	b.set_column_width(5,6,25)
	
	rows = (1,100)
	cols = (2,6)
	b.set_horizontal_align(rows, cols, "center")
	
	b.set_font_bold(2, 1, "on")
	b.set_font_bold(12, 1, "on")
	
	
	rows = (2,2)
	cols = (1,6)
	b.set_sheet_merge("TabG2", rows, cols) 
	rows = (12,12)
	b.set_sheet_merge("TabG2", rows, cols) 

	
end


********** "E:\yungyu\lottery_baby\do\JoLE R&R\Golosov\Tab3.1_1Mup.do"

do "E:\yungyu\lottery_baby\do\set_environment.do"

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "tot_redeem_lotto tot_redeem_receipt"


clear

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
keep if inrange(prize10M,0.1,5) //1M - 50M

gen v1 = pers_real_prize_aftertax if norm == 0
egen prize10M_realized = mean(v1), by(houseid)
replace prize10M_realized = prize10M_realized/10000
replace prize10M_realized = 0 if norm < 0
recode prize10M_realized . = 0
gen givebirth = baby > 0

gen notbase = norm != -1
gen Z = current == 1 & notbase == 1

* First Stage
reg prize10M_realized current notbase Z $control1 $control2 $control3 i.age i.year if norm == -1 | norm == 1 , cl(houseid) 

foreach y in totbaby givebirth{
    cap rm "$table/temp/Tab3.1_1Mup_`y'.xls"
    cap rm "$table/temp/Tab3.1_1Mup_`y'.txt"
	
	* Pooled Results
	forv i = 0(1)6{
		if `i' != -1{
			ivreg2 `y' current notbase (prize10M_realized=Z) $control1 $control2 $control3 i.age i.year if norm == -1 | norm == `i' , cl(houseid) 
			outreg2 using "$table/temp/Tab3.1_1Mup_`y'.xls" , append bd(3) sd(3) nocon keep(prize10M_realized) ctitle(`y',T`i',Pooled)

		}
	}
	
	* Cohort by cohort
	forv c = 96(1)101{
		forv i = -3(1)6{
			if `i' != -1{
				ivreg2 `y' current notbase (prize10M_realized=Z) $control1 $control2 $control3 i.age i.year if (norm == -1 | norm == `i') & win_year == `c', cl(houseid) 
				outreg2 using "$table/temp/Tab3.1_1Mup_`y'.xls" , append bd(3) sd(3) nocon keep(prize10M_realized) ctitle(`y',T`i',C`c')

			}
		}
	}
}

foreach y in totbaby givebirth{
	import delimited using "$table/temp/Tab3.1_1Mup_`y'.txt", clear

	sxpose, clear
	rename _var6 b
	rename _var7 se
	rename _var3 t
	rename _var4 cohort
	rename _var9 n

	keep cohort t b se n

	drop in 1

	gen b_destring = subinstr(b,"*","",.)
	destring b_destring, replace
	replace n = subinstr(n,",","",.)
	destring n, replace

	gen v1 = b_destring if cohort != "Pooled"
	replace v1 = v1 * n
	egen b_weighted = sum(v1), by(t)
	sum n if cohort == "Pooled"
	replace b_weighted = b_weighted/r(mean)
	tostring b_weighted, format(%4.3f) replace force
	tostring n, format(%9.0fc) replace force

	keep if cohort == "Pooled"
	keep t b se n b_weighted

	save "$table/temp/Tab3.1_1Mup_`y'.dta", replace
}

use "$table/temp/Tab3.1_1Mup_totbaby.dta", clear
loc N = _N+1
set obs `N'
ap using "$table/temp/Tab3.1_1Mup_givebirth.dta"
gen row = _n
loc N = _N+2
set obs `N'
recode row . = 0
sort row
drop row

replace t = "Event Time" in 1
replace b = "Beta^DD (Pooled)" in 1
replace se = "SE^DD (Pooled)" in 1
replace n = "# of Observations" in 1
replace b_weighted = "Beta^DD (Cohort-Weighted)" in 1

replace t = "Panel A: Cumulative Number of Children" in 2
replace t = "Panel B: Give Birth in a Given Year" in 10

export excel "$table/Tables_Replicate_Golosov.xlsx", sheet("Tab3.1 Above 1M") sheetreplace

mata
	b = xl()
	b.load_book("$table/Tables_Replicate_Golosov.xlsx")
	b.set_sheet("Tab3.1 Above 1M")

	b.set_column_width(1,1,12)
	b.set_column_width(2,4,20)
	b.set_column_width(5,5,25)
	
	rows = (1,100)
	cols = (2,5)
	b.set_horizontal_align(rows, cols, "center")
	
	b.set_font_bold(2, 1, "on")
	b.set_font_bold(10, 1, "on")
	
	rows = (2,2)
	cols = (1,5)
	b.set_sheet_merge("Tab3.1 Above 1M", rows, cols) 
	rows = (10,12)
	b.set_sheet_merge("Tab3.1 Above 1M", rows, cols) 
	
end

********** "E:\yungyu\lottery_baby\do\JoLE R&R\Golosov\Tab3.1_1Mup _ bootstrap.do"

do "E:\yungyu\lottery_baby\do\set_environment.do"

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "tot_redeem_lotto tot_redeem_receipt"

clear


qui{
	clear
	set obs 6 //N of cohort
	gen win_year = 95 + [_n]
	expand 2
	bysort win_year: gen y = _n
	tostring y, replace
	replace y = "givebirth" if y == "1"
	replace y = "totbaby" if y == "2"
	expand 9
	bysort y win_year: gen parm = _n
	gen coef = .
	gen se = .
	gen n = .
	expand 1001
	bysort y win_year parm: gen id = _n-1

	sort id y win_year parm

	save "$wdata\Golosov_Tab3.1_bootstrap.dta", replace
}


forv a = 0(1)1000{ //Bootstrap for 1,000 times
	
	if (`a'>0) dis "-- Bootstrap `a'..."
	{
	set seed `a'

	use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	keep if inrange(prize10M,0.1,5) //1M - 50M
	
	if (`a'>0) {
		keep if norm == 0
		keep win_year current houseid 
		bsample, str(win_year current)
		joinby houseid using "$wdata\project_lottery_fertility_b3a6_first_5k.dta"
	}
	
	gen v1 = pers_real_prize_aftertax if norm == 0
	egen prize10M_realized = mean(v1), by(houseid)
	replace prize10M_realized = prize10M_realized/10000
	replace prize10M_realized = 0 if norm < 0
	recode prize10M_realized . = 0
	gen givebirth = baby > 0

	gen notbase = norm != -1
	gen Z = current == 1 & notbase == 1

	foreach y in givebirth totbaby {
		
		* Cohort by cohort
		forv c = 96(1)101{
			forv i = 0(1)6{
				loc j = `i'+3
				if `i' != -1{
					qui ivreg2 `y' current notbase (prize10M_realized=Z) $control1 $control2 $control3 i.age i.year if (norm == -1 | norm == `i') & win_year == `c', cl(houseid) 
					
					local n`c' = e(N)
					matrix b = e(b)
					matrix V = e(V)
					
					local coef`y'_`c'_`j' = b[1,1]
					local se`y'_`c'_`j' =  sqrt(V[1,1])		
					


				}
			}
		}
	}

	use "$wdata\Golosov_Tab3.1_bootstrap.dta", clear

	foreach y in givebirth totbaby {	
		forv c = 96(1)101{
			replace n = `n`c'' if win_year == `c' & id == `a'
			forv i = 3(1)9{
				replace coef = `coef`y'_`c'_`i'' if y == "`y'" & win_year == `c' & parm == `i' & id == `a'
				replace se = `se`y'_`c'_`i'' if y == "`y'" & win_year == `c' & parm == `i' & id == `a'
			}
		}
	}

	save "$wdata\Golosov_Tab3.1_bootstrap.dta", replace
	}
}

use "$wdata\Golosov_Tab3.1_bootstrap.dta", clear

collapse (mean)coef [fw=n], by(y parm id)
drop if coef == .

replace parm = parm - 3

gen b2 = coef if id == 0
gen se2 = coef if id != 0

collapse (mean)b (sd)se, by(y parm)

gen row = _n
replace row = row + 20 if y == "givebirth"
loc N= _N+3
set obs `N'
replace row = 0 if row == .
replace row = 15 if _n == _N

sort row
drop row
	
save "$wdata\Golosov_Tab3.1_bootstrap_result.dta", replace

use "$table/temp/Tab3.1_1Mup_totbaby.dta", clear
loc N = _N+1
set obs `N'
ap using "$table/temp/Tab3.1_1Mup_givebirth.dta"
gen row = _n
loc N = _N+2
set obs `N'
recode row . = 0
sort row
drop row

merge 1:1 _n using "$wdata\Golosov_Tab3.1_bootstrap_result.dta"
tostring b2 se2, replace format(%4.3f) force
drop _m

replace t = "Event Time" in 1
replace b = "Beta^DD (Pooled)" in 1
replace se = "SE^DD (Pooled)" in 1
replace n = "# of Observations" in 1
replace b2 = "Beta^DD (Cohort-Weighted)" in 1
replace se2 = "SE^DD (Cohort-Weighted)" in 1

drop b_w y parm

replace t = "Panel A: Cumulative Number of Children" in 2
replace t = "Panel B: Give Birth in a Given Year" in 10

export excel "$table/Tables_Max50M.xlsx", sheet("TabG3") sheetreplace

mata
	b = xl()
	b.load_book("$table/Tables_Max50M.xlsx")
	b.set_sheet("TabG3")

	b.set_column_width(1,1,12)
	b.set_column_width(2,4,20)
	b.set_column_width(5,6,25)
	
	rows = (1,100)
	cols = (2,6)
	b.set_horizontal_align(rows, cols, "center")
	
	b.set_font_bold(2, 1, "on")
	b.set_font_bold(10, 1, "on")
	
	rows = (2,2)
	cols = (1,6)
	b.set_sheet_merge("TabG3", rows, cols) 
	rows = (10,10)
	b.set_sheet_merge("TabG3", rows, cols) 
	
end
